package IOstreams;

import java.io.*;

public class ObjectClone {
    public static void main(String[] args) {
        try (
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)
        ) {
            User user = new User("张三", new Address("河南", 11));
            objectOutputStream.writeObject(user);
            objectOutputStream.flush();
            try (
                 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
                 ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
                ){
                User readObject = (User) objectInputStream.readObject();
                System.out.println("readObject = " + readObject);
                user.setName("李四");
                user.getAddress().setAddressName("北京");
                System.out.println("readObject = " + readObject);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
